package com.monkeyboy.list;

import com.monkeyboy.ListNode;

/**
 * 两个链表的第一个公共结点
 *
 * @Author Gavin
 * @date 2021.04.05 12:30
 */
public class List09 {
    public ListNode findCommon(ListNode p1, ListNode p2) {
        ListNode l1 = p1;
        ListNode l2 = p2;
        while (l1 != l2) {
            l1 = (l1 == null) ? p2 : l1.next;
            l2 = (l2 == null) ? p1 : l2.next;
        }
        return l1;
    }
}
